﻿Shader "Custom/TestShader12"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
        _Bump("Normal", 2D) = "bump" {}
        _Cube("Cube", Cube) = "_Skybox" {}
        _ReflectAmount("_ReflectAmount", range(0,1)) = 0.5
        _Distortion("Distortion", range(0,1)) = 50
    }
        SubShader
    {
        Tags { "RenderType" = "Opaque" "Queue" = "Transparent" }

        GrabPass {"_RefractionTex"}
        Pass
        {
            Tags {"LightMode" = "ForwardBase"}
            CGPROGRAM
            #pragma multi_compile_fwdbass
            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _Bump;
            float4 _Bump_ST;
            samplerCUBE _Cube;
            sampler2D _RefractionTex;
            float4 _RefractionTex_TexelSize;
            float _ReflectAmount;
            float _Distortion;

            struct a2v
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 texcoord : TEXCOORD0;
                float4 tangent : TANGENT;
            };

            struct v2f
            {
                float4 pos : POSITION;
                float4 uv : TEXCOORD0;
                float4 T2W0 : TEXCOORD1;
                float4 T2W1 : TEXCOORD2;
                float4 T2W2 : TEXCOORD3;
                float4 scrPos : TEXCOORD4;

            };

            v2f vert(a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                o.uv.zw = v.texcoord.xy * _Bump_ST.xy + _Bump_ST.zw;
                o.scrPos = ComputeGrabScreenPos(o.pos);

                float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
                float3 worldNormal = UnityObjectToWorldNormal(v.normal);
                float3 tangent = normalize(UnityObjectToWorldDir(v.tangent)).xyz;
                float3 binormal = cross(normalize(worldNormal), normalize(tangent)) * v.tangent.w;


                o.T2W0 = float4(tangent.x, binormal.x, worldNormal.x,  worldPos.x);
                o.T2W1 = float4(tangent.y, binormal.y, worldNormal.y,  worldPos.y);
                o.T2W2 = float4(tangent.z, binormal.z, worldNormal.z,  worldPos.z);

                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed3 worldPos = float3(i.T2W0.w, i.T2W1.w, i.T2W2.w);
                fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));

                fixed3 normal = UnpackNormal(tex2D(_Bump, i.uv.zw));
                float2 offset = normal.xy * _RefractionTex_TexelSize.xy * _Distortion;
                i.scrPos.xy += offset.xy;
                fixed3 refrCol = tex2D(_RefractionTex, i.scrPos.xy / i.scrPos.w).rgb;

                fixed3 worldNormal = normalize(fixed3(dot(i.T2W0.xyz, normal), dot(i.T2W1.xyz, normal), dot(i.T2W2.xyz, normal)));
                fixed3 reflectDir = reflect(-worldViewDir, worldNormal);

                fixed3 mainTexColor = tex2D(_MainTex, i.uv.xy).rgb;
                fixed3 reflCol = texCUBE(_Cube, reflectDir).rgb *mainTexColor;

                return fixed4(reflCol * _ReflectAmount + (1- _ReflectAmount) * refrCol, 1);

            }

            ENDCG
		}
	}
}
